<?php

namespace app\common\dao\system;

use app\common\model\system\Category as model;
use app\common\dao\BaseDao;
use app\common\repositories\system\CategoryRepository;
use basic\traits\CategoresDao;

class CategoryDao extends BaseDao
{
    use CategoresDao;

    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }

    public function fieldExistsList($type,$field,$value,$except = null)
    {
        return ($this->getModel()::getDB())->when($except ,function($query)use($field,$except){
            $query->where($field,'<>',$except);
        })->where($field,$value)->where('type',$type);

    }

    public function getTwoLevel($type)
    {
        $pid = model::getDB()->where('pid', 0)->where('type',$type)->column('category_id');
        return model::getDB()->whereIn('pid', $pid)->where('is_show', 1)->where('type',$type)->order('sort DESC')->column('category_id,cate_name,pid,type');
    }

    public function children($pid)
    {
        return model::getDB()->where('pid', $pid)->where('is_show', 1)->order('sort DESC')->column('category_id,cate_name,pic,type');
    }

    public function getTypeExsit($type,$id)
    {
        return model::getDB()->where(['type'=>$type,'category_id'=>$id,'is_show'=>1])->count();
    }

    public function getAllChildren($id)
    {
        $path = model::getDB()->where('category_id', $id)->value('path');
        $path .=$id.'/';
        return model::getDB()->whereLike('path', "$path%")->order('sort DESC')->column('category_id,cate_name,pid,pic,type');
    }

    public function getAllShowChildren($id)
    {
        $path = model::getDB()->where('category_id', $id)->value('path');
        $path .=$id.'/';
        return model::getDB()->whereLike('path', "$path%")->where('is_show',1)->order('sort DESC')->column('category_id,cate_name,pid,pic,type');
    }

    public function getMaxLevel($type = null)
    {
        $levelMap = app()->make(CategoryRepository::class)::TYPE_MAX_LEVEL;
        return $levelMap[$type]??3;
    }

    public function getAll($type,$status = null,$hidden = [])
    {
        return $this->getModel()::getDB()->where('type', $type)->when(($status !== null),function($query)use($status){
            $query->where($this->getStatus(),$status);
        })->order('sort DESC')->hidden($hidden)->select();
    }


    public function getAllWithWorkType($type,$status = null,$hidden = [])
    {
        return $this->getModel()::getDB()->where('type', $type)->when(($status !== null),function($query)use($status){
            $query->where($this->getStatus(),$status);
        })->order('sort DESC')->hidden($hidden)->select()->append(['work_type']);
    }


    public function getAllData($type,$status = null,$level = null)
    {
        $field = $this->getParentId().',cate_name';
        $query = ($this->getModel()::getDB());
        $query->where('type',$type)
            ->when($status,function($query)use($status){
                $query->where($this->getStatus(),$status);
            })
            ->when(($level !== null),function($query)use($level){
                $query->where($this->getLevel(),'<',$level);
            });
        return $query->order('sort DESC')->column($field, $this->getPk());
    }


    public function getCompleteName($id,$str = '/')
    {
        $cate1 = $this->get($id);
        $cate2 = $this->get($cate1->pid??0);
        $cate3 = $this->get($cate2->pid??0);

        return ($cate3?($cate3->cate_name.$str):"").
            ($cate2?($cate2->cate_name.$str):"").
            ($cate1?$cate1->cate_name:"");
    }



}